﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports System.IO
Imports Boogaloo.Core.UI
Imports Boogaloo.Admin.UI.Controls
Imports Boogaloo.Core
Imports Boogaloo.Core.BLL


Namespace UI.Webpages


    Partial Public Class DesignFiles
        Inherits BaseAdminPage

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            If Not Page.IsPostBack Then
                Dim dirs As String() = Groove.GetFullDirectoryTree(Server.MapPath(CoreSettings.GetSetting("SITE_MastersDirectory")))
                Dim dirName As String = String.Empty
                For Each dirName In dirs
                    ddl_dir.Items.Add(New ListItem(Groove.RewriteServerPathAsVirtualPath(dirName)))
                Next

                BindPage()
            End If

        End Sub


        Private Sub BindPage()

            rpt_files.Visible = True
            rpt_mastertemplates.Visible = True

            Dim dirFiles As String() = Directory.GetFiles(Server.MapPath(ddl_dir.SelectedValue), "*.master")
            Dim bindFiles As New ArrayList
            Dim mtDict As Dictionary(Of String, MasterTemplate) = MasterTemplate.GetMasterTemplatesDictionary(Groove.admin_root.root_id)

            Dim fn As String = String.Empty
            For Each fn In dirFiles
                If Not mtDict.ContainsKey(fn) Then bindFiles.Add(fn)
            Next

            rpt_files.DataSource = bindFiles
            rpt_files.DataBind()
            If rpt_files.Items.Count = 0 Then
                rpt_files.Visible = False
                lit_nofiles.Visible = True
            Else
                lit_nofiles.Visible = False
            End If

            rpt_mastertemplates.DataSource = Groove.admin_root.master_templates()
            rpt_mastertemplates.DataBind()

            If rpt_mastertemplates.Items.Count = 0 Then
                rpt_mastertemplates.Visible = False
                lit_nomasterpages.Visible = True
            Else
                lit_nomasterpages.Visible = False
            End If

            


        End Sub

        


        Protected Sub ddl_dir_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddl_dir.SelectedIndexChanged

            BindPage()

        End Sub


        Protected Sub rpt_files_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles rpt_files.ItemDataBound

            Dim fileName As String = CType(e.Item.DataItem, String)

            If fileName IsNot Nothing Then
                Dim info As New FileInfo(fileName)

                Dim lit_filename As Literal = CType(e.Item.FindControl("lit_filename"), Literal)
                lit_filename.Text = info.Name

                Dim txt_alias As TextBox = CType(e.Item.FindControl("txt_alias"), TextBox)
                txt_alias.Text = info.Name

                Dim rfv_alias As RequiredFieldValidator = CType(e.Item.FindControl("rfv_alias"), RequiredFieldValidator)
                rfv_alias.ValidationGroup = info.Name

                Dim lb_Add As LinkButton = CType(e.Item.FindControl("lb_add"), LinkButton)
                lb_Add.ValidationGroup = info.Name
                lb_Add.CommandArgument = e.Item.ItemIndex

            End If

        End Sub


        Protected Sub rpt_mastertemplates_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles rpt_mastertemplates.ItemDataBound

            Dim mt As MasterTemplate = CType(e.Item.DataItem, MasterTemplate)

            If mt IsNot Nothing Then

                Dim validationgroup As String = String.Format("editalias{0}", e.Item.ItemIndex)

                Dim lht_alias As LabelHiddenTextbox = CType(e.Item.FindControl("lht_alias"), LabelHiddenTextbox)
                lht_alias.label = mt.name
                'spht_alias.label = "(Click to edit)"
                lht_alias.Text = mt.name
                lht_alias.CommandArgument = mt.master_template_id
                lht_alias.ValidationGroup = validationgroup
                CType(e.Item.FindControl("lit_master_virtual_path"), Literal).Text = mt.master_virtual_path

                Dim dwiz As CommitWizard = CType(e.Item.FindControl("dwiz"), CommitWizard)
                dwiz.CommandArgument = mt.master_template_id
                If mt.nodes.Count > 0 Then dwiz.Visible = False

            End If

        End Sub

        Protected Sub lb_add_Click(ByVal sender As Object, ByVal e As EventArgs)

            If Page.IsValid Then
                Dim index As Integer = CType(CType(sender, LinkButton).CommandArgument, Integer)

                Dim filePath As String = String.Empty
                Dim name As String = String.Empty
                With rpt_files.Items(index)
                    filePath = String.Format("~{0}{1}", ddl_dir.SelectedValue, CType(.FindControl("lit_filename"), Literal).Text)
                    name = CType(.FindControl("txt_alias"), TextBox).Text
                End With
                Dim master_template_id As Integer = MasterTemplate.InsertMasterTemplate(name, filePath, Groove.admin_root.root_id)
            End If

            ' Needed so that ajax refresh does not get in the way of displaying the 
            ' Site map opener link.
            If Groove.admin_root.master_templates().Count = 1 Then
                Groove.RefreshPage()
            Else
                BindPage()
            End If


        End Sub



        Protected Sub lht_alias_Click(ByVal sender As Object, ByVal e As EventArgs)

            If Page.IsValid Then

                Dim lht As LabelHiddenTextbox = CType(sender, LabelHiddenTextbox)
                Dim master_template_id As Integer = CType(lht.CommandArgument, Integer)
                Dim mt As MasterTemplate = MasterTemplate.GetMasterTemplate(master_template_id)
                mt.name = lht.Text
                mt.Update()

                BindPage()

            End If

        End Sub


        Protected Sub dwiz_Click(ByVal sender As Object, ByVal e As EventArgs)

            Dim dwiz As CommitWizard = CType(sender, CommitWizard)
            Dim master_template_id As Integer = CType(dwiz.CommandArgument, Integer)

            Dim success As Boolean = MasterTemplate.DeleteMasterTemplate(master_template_id)
            Response.Redirect(Request.Url.AbsoluteUri)

        End Sub

    End Class


End Namespace